import {
  chatGroup
} from '@/config/wsRequest.js'
const state = {
  chatGroup: [], // 聊天组数据
  readCounts: 0, // 消息未读数
}
const mutations = {
  SET_CHATGROUP: (state, data) => {
    state.chatGroup = data
  },
  SET_READCOUNTS: (state, data) => {
    state.readCounts = data
  }
}

const actions = {
  ChatGroup({ commit }, id) {
    return new Promise(async(resolve) => {
      const { code, data } = await chatGroup({ id, userType: 'user' })
      if (code === 0) {
        const readCounts = data.map(item => item.readCount)
        commit('SET_READCOUNTS', readCounts.length > 0 ? readCounts.reduce((prev,current) => prev + current) : 0 )
        commit('SET_CHATGROUP', data)
        resolve(code)
      }
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}